package com.ljkj.common.db.util;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import flexjson.*;
import flexjson.transformer.*;

public class JSONUTIL {
	public static String Encode(Object obj) {
		if(obj == null || obj.toString().equals("null")) return null;
		if(obj != null && obj.getClass() == String.class){
			return obj.toString();
		}
		JSONSerializer serializer = new JSONSerializer();
		serializer.transform(new DateTransformer("yyyy-MM-dd'T'HH:mm:ss"), Date.class);
		serializer.transform(new DateTransformer("yyyy-MM-dd'T'HH:mm:ss"), Timestamp.class);
		return serializer.deepSerialize(obj);
	}
	public static Object Decode(String json) {
		if (StringUtil.isNullOrEmpty(json)) return "";
		JSONDeserializer deserializer = new JSONDeserializer();
		deserializer.use(String.class, new DateTransformer("yyyy-MM-dd'T'HH:mm:ss"));		
		Object obj = deserializer.deserialize(json);
		if(obj != null && obj.getClass() == String.class){
			return Decode(obj.toString());
		}		
		return obj;
	}
	
	/**
	 * 该方法主要针对页面dtatgrid url传递过来的参数进行处理。
	 * @param andStr
	 * @return
	 */
	public static String JsonFromString(String andStr){
		
		if(andStr != null && !"".equals(andStr)){
			if(andStr.indexOf("&")!= -1){
				String[] array = andStr.split("&");
				Map<String, String> map = new HashMap<String, String>();
				for(String strTemp : array){
					if(strTemp.indexOf("=") != -1){
						String[] strMap = strTemp.split("=");
						
						if(strMap.length == 1){
							map.put(strMap[0], "");
						}else{
							map.put(strMap[0], strMap[1]);
						}
					}
				}
				return JSON.toJSONString(map);
			}
		}
		return andStr;
	}
}

